(function($){
	
	var openDays = 2;
	
	
	var buildParams4Pw =function(){
		var empCode=getEmpCode();
		//客户经理的电话号码
		var mobile = getMobile();
		//客户号码
		var phone = $("#phone").val();
		var validateType = $("#validateType").val();
		var validateCode = $("#validateCode").val();
		//alert(mobile);
        var params = {"BODY":{"OLD_SERVER": "s1246Esop","SERVICE_NO": phone,"PROVINCE_GROUP": "10008","LOGIN_NO": empCode,"WORK_NO": empCode,"PHONE_NO": phone,"VERIFY_TYPE": validateType,"VERIFY_CODE": validateCode,"OP_CODE": "1246"}};
	    return params;
	};
	
	var buildParams4Auth =function(){
		
        var params = {"token":getParamValue("token"),"staffNo": getEmpCode(),"passUrl":"phoneNo="+ $("#phone").val()};
	    return params;
	};
	
	var buildParams4Suspend =function(){
		var empCode=getEmpCode();
		//客户号码
		var phone =  $("#phone").val();
		var remark = $("#remark").val();
	    var runCode= $("#operateType").val();
	    var op_type= (runCode=="A")?"OPEN":"CSTOP";
		var days = $("#days").val()==""?"0":$("#days").val();
		var finalDays = (runCode=="A")?days:"0";
		 //TODO: 去掉PHONE_NO
        var params = {"BODY": {"PHONE_NO": getMobile(),"OLD_SERVER": "s1246CfmCM","SERVICE_NO": phone,"PROVINCE_GROUP": "10008","LOGIN_NO": empCode,"LOGIN_PWD": "","MASTER_SERV_ID": "1001","OP_TYPE": op_type,"STOP_TIME": finalDays,"CONTACT_ID": "8888","IP_ADDRESS": "10.95.251.24","REMARK": remark,"RUN_CODE": runCode}};
	    return params;
	};
	
	var check = function(){
		var phone = $("#phone").val();
		var validateType = $("#validateType").val();
		var validateCode = $("#validateCode").val();
		if((phone==null)||(phone=="")||(phone.length!=11)){
			alert("请输入11位移动手机号码");
			return false;
		}
		if(!checkCmccPhone(phone)){
			alert("仅支持移动手机号码！");
			return false;
		}
		if(validateType==0){
			alert("请选择校验类型");
			return false;
		}
		if((validateCode==null)||(validateCode=="")){
			alert("客户密码或身份证不能为空");
			return false;
		}
		
		return true;
	}
	
	var suspendResume = function(){
		//showLoadingBar($("#loadingbar"),true,false,"正在进行请求，请稍后……");
		showBtnProgress($("#do"),true,"办理中……");
	  	var params = buildParams4Suspend();
	  	
	 	$.ajax({
		    type: "post",
		    contentType: "application/json",
		    data: JSON.stringify(params),
		    url: Constants.rpcCrmHost+"s2112CfmL?token="+getToken(),
		    dataType: "json",
		    async: true,
		    headers: {
      'token': getParamValue('token'),
      'saleType': getParamValue('saleType')
    },
    success: function(data) {
      if (data.ResultCode === 1009) {
        alert('登陆超时，请重新登录！');
        jumpToSignin();
        return;
      }
		    	showBtnProgress($("#do"),false,"确  认");
		    	console.log("data = "+data);
		        console.info(data);
		        if(data.ROOT.RETURN_CODE!=0){
		        	showLoadingBar($("#loadingbar"),true,true,data.ROOT.RETURN_MSG);
		        	console.log("获取RPC提醒信息接口返回失败！code:"+data.ROOT.RETURN_CODE);
		        	return;
		        }
		        showLoadingBar($("#loadingbar"),true,true,"业务办理成功");
		        saveOrder(params);
		    },
		    error : function(textStatus) {
		    	showBtnProgress($("#do"),false,"确  认");
		    	showLoadingBar($("#loadingbar"),true,true,"业务办理失败！服务器没有返回。");
		        console.info("request error:");
		    }
		  });
	}
	var saveOrder = function(params) {
    console.log("saveOrder参数：", params);
    $.ajax({
        type: "post",
        contentType: "application/json",
        data: JSON.stringify(params),
        url: Constants.apiV2Host+"h5/order/suspend/save",
        dataType: "json",
        async: true,
        headers: {
      'token': getParamValue('token'),
      'saleType': getParamValue('saleType')
    },
    success: function(data) {
      if (data.ResultCode === 1009) {
        alert('登陆超时，请重新登录！');
        jumpToSignin();
        return;
      }
          if ('' + data.ResultCode !== '1000') {
            console.log(data.Message);
            alert("保存订单失败！");
          }
        },
        error: function(textStatus) {
          alert("保存订单失败！服务器没有返回。");
          console.info("request error:" + textStatus);
        }
    })
  }
	
	var showDetails=function(data){
		hide($("#validator"));
		$("#custName").html(data.CUST_NAME);
		$("#contact").html(data.PHONE_NO);
		$("#master_serv_name").html(data.MASTER_SERV_NAME);
		$("#cust_addr").html(data.CONTACT_ADDRESS);
		$("#stateCode").html(data.RUN_CODE);
		$("#operateAvailable").html(data.NEW_RUN_NAME);
		$("#idType").html(data.ID_TYPE);
		$("#id").html(data.ID_ICCID);
		$("#runState").html(data.RUN_CODE_NAME);
		openDays =data.EXPIRE_DAYS;
		
		

		
		

		

		 
/*		$("#").html();
		$("#").html();
		$("#").html();*/
		
		show($("#request"));
	}
	
	var validatePw = function(){
		//showLoadingBar($("#loadingbar"),true,false,"正在校验输入的用户信息，请稍后……");
	  	params = buildParams4Pw();
	  	
	 	$.ajax({
		    type: "post",
		    contentType: "application/json",
		    data: JSON.stringify(params),
		    url: Constants.rpcCrmHost+"sSmsQryScL?token="+getToken(),
		    dataType: "json",
		    async: true,
		    headers: {
      'token': getParamValue('token'),
      'saleType': getParamValue('saleType')
    },
    success: function(data) {
      if (data.ResultCode === 1009) {
        alert('登陆超时，请重新登录！');
        jumpToSignin();
        return;
      }
		    	showBtnProgress($("#validate"),false,"验证");
		    
		    	console.log("data = "+data);
		        console.info(data);
		        if(data.ROOT.RETURN_CODE!=0){
		        	showLoadingBar($("#loadingbar"),true,true,data.ROOT.RETURN_MSG);
		        	console.log("获取RPC提醒信息接口返回失败！code:"+data.ROOT.RETURN_CODE);
		        	return;
		        }
		        //showLoadingBar($("#loadingbar"),false);
		        showDetails(data.ROOT.OUT_DATA);
		    },
		    error : function(textStatus) {
		    	showLoadingBar($("#loadingbar"),true,true,"验证失败！服务器没有返回。");
		    	showBtnProgress($("#validate"),false,"验证");
		        console.info("request error:");
		    }
		  });
    };
    
	var validateAuth = function(){
		//showLoadingBar($("#loadingbar"),true,false,"正在校验该集团与工号是否匹配，请稍后……");
		showBtnProgress($("#validate"),true,"验证中……");
	  	params = buildParams4Auth();
	  	
	 	$.ajax({
		    type: "post",
		    contentType: "application/json",
		    data: JSON.stringify(params),
		    url: Constants.rpcEsopHost+"qryUnitByPhoneNo",
		    dataType: "json",
		    async: true,
		    headers: {
      'token': getParamValue('token'),
      'saleType': getParamValue('saleType')
    },
    success: function(data) {
      if (data.ResultCode === 1009) {
        alert('登陆超时，请重新登录！');
        jumpToSignin();
        return;
      }
		    	console.log("data = "+data);
		        console.info(data);
		        if(data.rtnCode!=1000){
		        	showLoadingBar($("#loadingbar"),true,true,data.rtnMsg);
		        	showBtnProgress($("#validate"),false,"验证");
		        	console.log("获取RPC接口返回失败！code:"+data.rtnCode);
		        	return;
		        }
		        if(data.return.resultCode!="00"){
		        	showLoadingBar($("#loadingbar"),true,true,data.return.resultDesc);
		        	showBtnProgress($("#validate"),false,"验证");
		        	console.log("获取信息接口返回失败！code:"+data.return.resultCode);
		        	return;
		        }
		        //showLoadingBar($("#loadingbar"),false);
		        
		        validatePw();
		    },
		    error : function(textStatus) {
		    	showLoadingBar($("#loadingbar"),true,true,"验证权限失败！服务器没有返回。");
		    	showBtnProgress($("#validate"),false,"验证");
		        console.info("request error:");
		    }
		  });
    };
	
	
    $(function(){
    	hide($("#request"));
    	show($("#validator"));
    	showLoadingBar($("#loadingbar"),false);
    	//show($("#request"));
    	//hide($("#validator"));
    	//showLoadingBar($("#loadingbar"),true);
     	$("#validate").on("click",function(){
    		if(check()){
    			validateAuth();
    		}
    	});
     	
     	$("#do").on("click",function(){
     		var operType=$("#operateType").val();
     		if(operType=="0"){
     			alert("请输入办理业务类型！");
     			return;
     		}
     		else if(operType=="A"){
     			var days=$("#days").val();
     			if((!days)||(days=="")){
     				alert("请输入强制开机天数");
         			return;
     			}
     			
     			if(!checkNumber(days)){
     				alert("强开天数必须是纯数字！");
         			return;
     			}
     			
     			if(days>openDays){
     				alert("强开天数不能超过"+openDays+"天");
         			return;
     			}
     			
     		}
     		suspendResume();
    	});
     	
	    if (!$.AMUI.support.mutationobserver) {
	    	$("#operateType").trigger('changed.selected.amui');
	    }
		
		$("#operateType").on('change', function() {
		  if($(this).val()=="A"){
			  $("#days").attr("placeholder","请输入小于或等于" + openDays+ "的天数");
			  show($("#date"));
		  }else{
			  hide($("#date"));
		  }
		});
  	
    });
  


})(jQuery);